package com.example.hunheandroid


import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.hunheandroid.databinding.ActivityGoFlutterBinding
import io.flutter.plugins.BasePlugin

import io.flutter.plugins.HostMessageApi


class GoFlutterActivity : AppCompatActivity(), HostMessageApi {
    private val tag = "GoFlutterActivity"
    private lateinit var binding: ActivityGoFlutterBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityGoFlutterBinding.inflate(layoutInflater)

        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)
        // 启用返回按钮
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
        supportActionBar?.title = "Go Flutter"
        binding.goFlutter.setOnClickListener {
            //跳转到flutter
            val initialRoute = listOf(  // Map 形式的参数
                "/",
                "aaaa"
            )
            BasePlugin.getFlutterMessageApi()?.native2Flutter(initialRoute.joinToString("#"), 1) {
                Log.e(tag, "flutter2NativeAsync: $it")
                FlutterLibrary.startFlutterPage(this)
            }

        }

        binding.goFlutter2.setOnClickListener {
            //跳转到flutter
            val initialRoute = listOf(  // Map 形式的参数
                "/page2",
                "bbbb"
            )
            BasePlugin.getFlutterMessageApi()?.native2Flutter(initialRoute.joinToString("#"), 1) {
                Log.e(tag, "flutter2NativeAsync: $it")
                FlutterLibrary.startFlutterPage(this)
            }

        }
    }

    override fun flutter2Native(message: String, type: Long): String {
        Log.e(tag, "$message$type")
        return "我是flutter调用原生方法的返回值=》flutter2Native"
    }

    override fun flutter2NativeAsync(
        message: String,
        type: Long,
        callback: (Result<String>) -> Unit
    ) {
        Log.e(tag, "$message$type")
        callback(Result.success("我是flutter调用原生方法的返回值=》flutter2NativeAsync"))

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                finish()  // 或者 finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

}